<?php
namespace app\api\controller;

use app\BaseController;
use app\JWT;
use think\Response;
use think\exception\HttpResponseException;

abstract class ApiBase extends BaseController{
    /**
     * 当前用户信息
     * @var array
     */
        protected $user = [];

    //__auth
    protected function __auth(){
        $token = $this->request->header('token');
        if(empty($token) || is_null($token)){
            $rs = json_encode(['code'=>401,'msg'=>'您还未登录或已过期，请先登录！']);
            $re = Response::create($rs)->header(header: ['Content-Type' => 'application/json']);
            throw new HttpResponseException($re);
        }
        $this->user = JWT::verifyJWT($token);
        if(!$this->user){
            $rs = json_encode(['code'=>401,'msg'=>'您还未登录或已过期，请先登录！']);
            $re = Response::create($rs)->header(header: ['Content-Type' => 'application/json']);
            throw new HttpResponseException($re);
        }
    }
}